package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.mapper.TbAddressMapper;
import com.dongdong.mapper.TbAreasMapper;
import com.dongdong.mapper.TbCitiesMapper;
import com.dongdong.mapper.TbProvincesMapper;
import com.dongdong.pojo.*;
import com.dongdong.service.AddressService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/22 16:48
 * @Version 1.0
 **/
@Service
@Component
public class AddressServiceImpl implements AddressService {
    
    @Resource
    private TbAddressMapper tbAddressMapper;

    @Resource
    private TbProvincesMapper provincesMapper;

    @Resource
    private TbCitiesMapper citiesMapper;

    @Resource
    private TbAreasMapper areasMapper;
    
    @Override
    public List<TbAddress> getAddress(String username) {
        List<TbAddress> addressList = tbAddressMapper.getAddress(username);
        for (TbAddress address : addressList) {
            String provinceName = provincesMapper.selectByProvinceId(address.getProvinceId());
            String cityName = citiesMapper.selectByCityId(address.getCityId());
            String townName = areasMapper.selectByAreasId(address.getTownId());
            address.setProvinceName(provinceName);
            address.setCityName(cityName);
            address.setTownName(townName);
        }
        return addressList;
    }

    @Override
    public void insertAddress(TbAddress address) {
        address.setCreateDate(new Date());
        address.setIsDefault("0");
        tbAddressMapper.insertSelective(address);
    }

    @Override
    public void deleteAddress(Long id) {
        tbAddressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void isDefault(String username, Long id) {
        tbAddressMapper.setDefault(username);
        tbAddressMapper.isDefault(username,id);
    }

    @Override
    public TbOrder getReceiver(Long addressId, TbOrder order) {
        TbAddress address = tbAddressMapper.selectByPrimaryKey(addressId);
        String ProvinceName = provincesMapper.selectByProvinceId(address.getProvinceId());
        String cityName = citiesMapper.selectByCityId(address.getCityId());
        String townName = areasMapper.selectByAreasId(address.getTownId());
        String address1 = address.getAddress();
        String receiver = ProvinceName + cityName +townName + address1;
        order.setReceiverAreaName(receiver);
        order.setReceiverMobile(address.getMobile());
        order.setReceiver(address.getContact());
        return order;
    }
}

